#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll a, n, len;
ll st[100], tt;

ll ishuiwen(ll num)
{
    ll x = num;tt = 0;
    while (x) {
        st[++tt] = x % a;
        x /= a;
    }
    ll l = 1, r = tt;
    while (l < r) {
        if (st[l] != st[r]) return 0;
        l++;r--;
    }
    return num;
}

ll tonum(string& t)
{
    ll num = 0;
    for (char ch : t) {
        num = num * 10 + ch - '0';
    }
    return num;
}

ll bfs()
{
    ll ans = 0;
    queue<string> q;
    string t = "0";
    for (ll i = 0;i <= 9;i++) {
        q.push(t);t[0]++;
    }
    t = "00";
    for (ll i = 0;i <= 9;i++) {
        q.push(t);
        t[0]++;t[1]++;
    }

    while (!q.empty()) {
        t = q.front();q.pop();

        if (t.front() != '0') {
            ll t_num = tonum(t);
            if (t_num <= n) ans += ishuiwen(t_num);
        }
        for (char ch = '0';ch <= '9';ch++) {
            string nx = ch + t + ch;
            if (nx.length() <= len)
                q.push(nx);
        }
    }
    return ans;
}

void solve()
{
    cin >> a >> n;
    len = to_string(n).length();
    cout << bfs();
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}